#!/bin/sh
# -------------------------------------------------------------------------------
# Version:      1.0
# Date:         2016/9/19
# Description:  Main info for koolshare Merlin tool.
# Author:       Nick Zhang
# Email:        thesadboy@qq.com
# WebSite:      http://wwww.microarticle.cn
# -------------------------------------------------------------------------------
ROOT_PATH='' #root path, default is ''
OUTER_PATH=$ROOT_PATH/jffs/merlin-web #outer path, merlin-web
DIR_PATH=$ROOT_PATH/jffs/merlin-web/device-map #dir path, device-map
SRC_PATH=$ROOT_PATH/www/device-map  #src path, /www
INIT_FILE=$ROOT_PATH/jffs/scripts/init-start  #init-start file
CMD_FILE=$ROOT_PATH/jffs/scripts/router-status  #router-status file
TOOL_URL=https://gitee.com/thesadboy/koolshare-merlin-web/raw/master/tools/router_status_tool.sh
REMOTE_FILES_0=https://gitee.com/thesadboy/koolshare-merlin-web/raw/master/device-map/router_status.asp
REMOTE_FILES_1=https://gitee.com/thesadboy/koolshare-merlin-web/raw/master/device-map/router_info_version.js
REMOTE_FILES_2=https://gitee.com/thesadboy/koolshare-merlin-web/raw/master/device-map/router_info_sys.js
MOUNT_CMD="mount --bind $DIR_PATH $SRC_PATH"
#logger print
logger(){
  echo "[NOTICE] $*."
}
#update tool file
update_tool(){
  wget -P /jffs/scripts -N $TOOL_URL --no-check-certificate && chmod 775 /jffs/scripts/router_status_tool.sh && /jffs/scripts/router_status_tool.sh update-file
}
#check file dir
check_dir(){
  if [ ! -d $DIR_PATH ];then
    logger "Path '$DIR_PATH' not exist, create";
    mkdir -p $DIR_PATH;
    logger "Copy source file to dir"
    cp -R $SRC_PATH/* $DIR_PATH
  fi
}
#download files
downlaod_files(){
  logger "Download file from $REMOTE_FILES_0"
  wget -P $DIR_PATH -N $REMOTE_FILES_0 --no-check-certificate
  logger "Download file from $REMOTE_FILES_1"
  wget -P $DIR_PATH -N $REMOTE_FILES_1 --no-check-certificate
  logger "Download file from $REMOTE_FILES_2"
  wget -P $DIR_PATH -N $REMOTE_FILES_2 --no-check-certificate
}
#add command startup
add_startup(){
  if [ ! -f $INIT_FILE ]; then
    logger "init-start file not exit, create"
    echo '#!/bin/sh' >> $INIT_FILE
    chmod 775 $INIT_FILE
  fi
  logger "Init router-status file"
  echo '#!/bin/sh' > $CMD_FILE
  echo $MOUNT_CMD >> $CMD_FILE
  chmod 775 $CMD_FILE
  grep "$CMD_FILE" $INIT_FILE > /dev/null
  if [ ! $? -eq 0 ];then
    logger "mount router-status cmd not found in init-start file, append"
    echo $CMD_FILE >> $INIT_FILE
    logger "mount file"
    eval ${MOUNT_CMD}
  fi
}
#remove command from init-start file
remove_cmd(){
  echo '#!/bin/sh' > $CMD_FILE
}

#remove files
remove_files(){
  rm -rf $OUTER_PATH
}

#install tool
install(){
  logger "Start install"
  check_dir
  downlaod_files
  add_startup
}

#uninstall tool
uninstall(){
  logger "Start uninstall, when finished, the router will reboot.."
  remove_cmd
  remove_files
  logger "Uninstall successful, system reboot.."
  reboot
}

#update tool
update(){
  logger "Start update the tool file"
  download_new_tool
}

#update files without tool file
updateFile(){
  logger "Start update files"
  downlaod_files
}


#Main
case $1 in
  install)
    install
    ;;
  uninstall)
    uninstall
    ;;
  update)
    update_tool
    ;;
  update-file)
    updateFile
    ;;
  -h|--help)
    echo "Usage: sh `basename $0` install    #Install this tool"
    echo "       sh `basename $0` uninstall  #Uninstall this tool"
    echo "       sh `basename $0` update     #Update this tool"
    echo "       sh `basename $0` -h,--help  #Print this help information"
    ;;
  *)
    echo "Usage: sh `basename $0` [install,update,remove,-h,--help]" && exit
    ;;
esac